package com.lxx.codegen;

import java.io.File;
import java.nio.file.Paths;
import java.util.Arrays;

public class GeneratorUtils {
	
	private GeneratorUtils() {
		
	}

	/**
	 * 根据模版返回生成的文件名
	 * @param tmplName 模版名称
	 * @param modelName 实体类对应的java bean名
	 * @return
	 */
	public static final String getGenerateFileName(String tmplName, String modelName) {
		tmplName = tmplName.replace("Model", modelName);
		tmplName = tmplName.replace(".ftl", "");
		return tmplName; 
	}

	/**
	 * 获取模版对应的目标文件路径
	 * @param prjPath 根目录
	 * @param packageName 包名
	 * @param tmplName 模版名
	 * @param modelName 实体类名
	 * @return
	 */
	public static final String getGenerateFilePath(String prjPath, String packageName, String tmplName,
			String modelName) {
		return joinPath(getPackagePath(prjPath, packageName), getGenerateFileName(tmplName, modelName));
	}
	
	/**
	 * 获取该包实际的文件路径
	 * @param packageName  包名 
	 * @param rootPackagePath 最顶层包的路径， 例如 .../src/main/java
	 * @return
	 */
	public static final String getPackagePath(String rootPackagePath, String packageName) {
		return joinPath(rootPackagePath, packageName.replace(".", File.separator));
	}
	
	/**
	 * 合并拼接文件路径
	 * @param paths 待合并文件路径数组
	 * @return
	 */
	public static final String joinPath(String ... paths) {
		if (paths.length == 0) {
			throw new IllegalArgumentException("paths can not be empty!");
		}
		
		if (paths.length == 1) {
			return paths[0];
		}
		
		return Paths.get(paths[0], Arrays.copyOfRange(paths, 1, paths.length)).toAbsolutePath().toString();
	}
	
	public static void main(String[] args) {
		System.out.println(joinPath("F:\\s/", "b"));
	}
}
